package com.sibu.yx.promotion.admin.web.support.filter;

import com.doubo.common.util.StringUtil;
import com.doubo.security.subject.Subject;
import com.sibu.yx.promotion.common.constants.AppConstants;
import com.sibu.yx.promotion.common.enums.LoginPlatEnum;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created with IntelliJ IDEA.
 * User: qiujingwang
 * Date: 2017/4/6
 * Description:
 */
@Slf4j
@Deprecated
public class TokenUidSubject implements Subject {

    private static final long serialVersionUID = -1655724323350159250L;
    protected volatile Boolean authenticated;
    protected Long uid;
    protected String token;
    protected HttpServletRequest request;
    protected HttpServletResponse response;

    protected LoginPlatEnum loginPlat;//登录渠道

    public TokenUidSubject(HttpServletRequest request, HttpServletResponse response) {
        this.request = request;
        this.response = response;
        init(request, response);
    }

    private void init(HttpServletRequest request, HttpServletResponse response) {
        token = request.getHeader(AppConstants.HEAD_TOKEN);//小程序token
        if (StringUtil.isNotBlank(token)) {
            String uidStr = request.getHeader(AppConstants.HEAD_UID);//小程序UID
            if(StringUtil.isNumeric(uidStr)){
                uid = Long.parseLong(uidStr);
            }
            loginPlat = LoginPlatEnum.FROM_XCX;
        }
    }

    @Override
    public boolean isAuthenticated() {
        if (authenticated == null) {
            if (uid != null) {
                Long tokenUid = 1L;
                authenticated = uid.equals(tokenUid);
            } else {
                authenticated = false;
            }
        }
        /*if (authenticated) {
            Integer platCode = MemberRedisUtil.getLoginPlat(token);
            if(null != platCode)
                loginPlat = LoginPlatEnum.resolve(platCode);
        }*/
        return authenticated;
    }

    @Override
    public Object getPrincipal() {
        return uid;
    }

    public Long getUid() {
        return uid;
    }

    public String getToken() {
        return token;
    }

    public LoginPlatEnum getLoginPlat() {
        return loginPlat;
    }
}
